const path = require("path");
const fs = require("fs");
const child = require("child_process");

let reserse = false;

if (process.argv.length >= 3) {
  reserse = process.argv[2] == "true";
}

const project = "F:\\js\\screeps";
const input = "/dist";
const screepsHome = "/temp";

let copy = function () {
  let src = path.join(project, input);
  // let output = path.join(project, screepsHome);
  let output = process.env.SCREEPS_HOME;

  if (reserse) {
    let temp = src;
    src = output;
    output = temp;
  }
  console.log("input", src);
  console.log("output", output);

  let srcList = fs.readdirSync(src);
  let outputList = fs.readdirSync(output);

  let op;
  //清理目标文件目录
  for (let o of outputList) {
    op = path.join(output, o);
    let stat = fs.statSync(op);
    if (stat.isFile()) {
      console.log("del ", op);
      fs.unlinkSync(op);
    }
  }
  //文件写入目标路径
  for (let i of srcList) {
    ip = path.join(src, i);
    op = path.join(output, i);
    let stat = fs.statSync(ip);
    if (stat.isFile()) {
      console.log("create ", op);
      fs.writeFileSync(op, fs.readFileSync(ip));
    }
  }
};

let run = function () {
  copy();
};

run();
